<template>
  <div class="login">
    <el-form ref="ruleFormRef" :model="page.form" size="large" :rules="rules">
      <h3 class="title">后台管理系统</h3>
      <el-form-item prop="username">
        <el-input
          v-model="page.form.username"
          placeholder="请输入用户名"
          :prefix-icon="UserFilled"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="page.form.password"
          placeholder="请输入密码"
          :prefix-icon="Lock"
          @keyup.enter="submit"
        />
      </el-form-item>
      <div class="fbc" style="margin-bottom: 1rem">
        <el-checkbox v-model="rememberMe" @change="rememberMeChange"
          >记住密码</el-checkbox
        >
        <span class="register">注册用户</span>
      </div>

      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          type="primary"
          style="width: 100%"
          @click="submit"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { UserFilled, Lock } from "@element-plus/icons-vue";
import type { FormInstance, FormRules } from "element-plus";
import { login } from "@/api/user";

const router = useRouter();
const ruleFormRef = ref<FormInstance>();
const page = reactive({
  form: {
    username: "admin",
    password: "123456",
  },
});

const rememberMe = ref(false);

const loading = ref(false);

const rules = reactive<FormRules>({
  username: [{ required: true, message: "请填写用户名", trigger: "blur" }],
  password: [
    { required: true, message: "请填写密码", trigger: "blur" },
    { min: 3, max: 18, message: "长度应在3-18位", trigger: "blur" },
  ],
});

onMounted(() => {
  rememberMe.value = sessionStorage.getItem("rememberMe") && sessionStorage.getItem("rememberMe") == "1" ? true : false
  if (rememberMe.value) {
    page.form.username = sessionStorage.getItem("username") as string
    page.form.password = sessionStorage.getItem("password") as string
  }
});

function rememberMeChange(e:Boolean) {
  sessionStorage.setItem("rememberMe", e ? "1" : "0");
  if(!e) {
    sessionStorage.removeItem("username")
    sessionStorage.removeItem("password")
  }
}

function submit() {
  ruleFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      loading.value = true;
      login(page.form)
        .then((res: any) => {
          sessionStorage.setItem("username",page.form.username)
          sessionStorage.setItem("password",page.form.password)
          let token = res.data.token;
          sessionStorage.setItem("token", token);
          router.push("/index");
        })
        .finally(() => {
          loading.value = false;
        });
    }
  });
}
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url("../assets/imgs/bg.jpg");
  background-size: cover;
  box-shadow: 0 0 4px 4px rgba(0, 0, 0, 0.3);
}
.el-form {
  width: 400px;
  border-radius: 6px;
  background: #fff;
  padding: 25px 25px 5px 25px;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.register {
  font-size: 14px;
  color: var(--el-color-primary);
  cursor: pointer;
}
</style>
